﻿// CMyDialog1.cpp: 实现文件
//
//类内变量time用来保存系统，类型为CTime。
//
#include "pch.h"
#include "StudentSystem.h"
#include "CMyDialog1.h"
#include "afxdialogex.h"
#include"resource.h"
#include "CMySheet.h"
#include"MyGridCell.h"

// CMyDialog1 对话框
IMPLEMENT_DYNAMIC(CMyDialog1, CDialogEx)
extern array<vector<int>, 7> repeatLec;
extern map<int, CLecture> Lecture;
CString week[8] = { _T("NULL"),   _T("Sun"), _T("Mon"), _T("Tue"), _T("Wed"), _T("Thu"), _T("Fri"), _T("Sat") };
CMyDialog1::CMyDialog1(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG1, pParent)
	
	, yearAndDay(_T(""))
	, hourAndMin(_T(""))
	, displayDate(_T(""))
{

}

CMyDialog1::~CMyDialog1()
{
}

void CMyDialog1::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CUSTOM1, CMyDialog1::m_grid1);
	DDX_Text(pDX, GLOBALDAY, yearAndDay);
	DDX_Text(pDX, GLOBALMIN, hourAndMin);
	DDX_Text(pDX, DISPLAY, displayDate);
	DDX_Control(pDX, DATETIMEPICK, datetimePick);
}


BEGIN_MESSAGE_MAP(CMyDialog1, CDialogEx)
	ON_COMMAND(DIALOG_TIMETABLE, &CMyDialog1::OnInitialDialog)
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_WM_CLOSE()
	ON_NOTIFY(DTN_DATETIMECHANGE, DATETIMEPICK, &CMyDialog1::OnDtnDatetimechangeDatetimepick)
	ON_NOTIFY(NM_CLICK, IDC_CUSTOM1, OnGridClick)
END_MESSAGE_MAP()


// CMyDialog1 消息处理程序


BOOL CMyDialog1::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	timerId = SetTimer(1, timeSpeed[curSpeed], NULL);
	time = dispTime = CTime::GetCurrentTime();
	// TODO: 在此添加额外的初始化代码
	CRect cr(0, 0, 1920, 1080);
	//CMyDialog1::m_grid1.GetWindowRect(&cr);//获得画图区域
	CMyDialog1::m_grid1.GetClientRect(&cr);
	int nRowNum = 49;
	CMyDialog1::m_grid1.SetColumnCount(8);//
	CMyDialog1::m_grid1.SetRowCount(nRowNum);//
	CMyDialog1::m_grid1.SetFixedRowCount(1);
	CMyDialog1::m_grid1.SetFixedColumnCount(1);//表头一行一列

	CString rowName, colName;
	for (int i = 0; i < nRowNum; i++)
	{
		if (i < 8) {
			CMyDialog1::m_grid1.SetColumnWidth(i, cr.Width() / 8);//设置列宽
			colName.Format("%d\n%s", dispTime.GetDay(), week[i]);
		}
		rowName.Format(_T("第%d行"), i);
		//CMyDialog1::m_grid1.SetItemText(0, i, colName);//设置内容
		CMyDialog1::m_grid1.SetRowHeight(i, cr.Height() / 12);//设置行高
		CMyDialog1::m_grid1.SetItemText(i, 0, rowName);
	}
	COLORREF clr = RGB(128, 128, 128);
	CMyDialog1::m_grid1.SetFixedTextColor(clr);//设置固定行的颜色
	CMyDialog1::m_grid1.SetFixedBkColor(RGB(128, 255, 255));//设置固定行的背景颜色
	CMyDialog1::m_grid1.SetRowHeight(0, 100);
	CMyDialog1::m_grid1.SetTextBkColor(clr);//设置可编辑区域背景颜色
	CMyDialog1::m_grid1.SetTextColor(RGB(0, 255, 255));//设置可编辑区域文字颜色
	CMyDialog1::m_grid1.SetEditable(false);//表格内容不可编辑设置
	updateDate();
	initCell();
	//CMyDialog1::m_grid1.AutoSizeRows();
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CMyDialog1::OnSize(UINT nType, int cx, int cy) {
	CDialog::OnSize(nType, cx, cy);
	m_WinSizeManage.ResizeWindow();
}
void CMyDialog1::OnInitialDialog()
{
	// TODO: 在此添加命令处理程序代码
}

void CMyDialog1::OnDestroy() {
	this->EndDialog(IDOK);//点击关闭后真正结束进程。
	KillTimer(1);
}
void CMyDialog1::OnClose() {
	CDialog::DestroyWindow();
}

void CMyDialog1::OnTimer(UINT_PTR idEvent) {
	CTimeSpan timeSpan(0, 0, 0, 1);
	time += timeSpan;
	yearAndDay = time.Format("%Y-%b-%d");
	hourAndMin = time.Format("%H:%M:%S");
	GetDlgItem(GLOBALDAY)->SetWindowText((yearAndDay));
	GetDlgItem(GLOBALMIN)->SetWindowText((hourAndMin));
}



//用户选择日期
void CMyDialog1::OnDtnDatetimechangeDatetimepick(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;

	datetimePick.GetTime(dispTime);
	displayDate.Format(_T("%d年 %d月"), dispTime.GetYear(), dispTime.GetMonth());
	GetDlgItem(DISPLAY)->SetWindowText((displayDate));
	//下面进行固定行的文字的替换。
	updateDate();
	return;
}

void CMyDialog1::OnGridClick(NMHDR* pNotifyStruct, LRESULT* /*pResult*/)
{
	NM_GRIDVIEW* pItem = (NM_GRIDVIEW*)pNotifyStruct;
	int row = pItem->iRow;
	int col = pItem->iColumn;
	CMyGridCell *myCell = (CMyGridCell* )m_grid1.GetCell(row, col); // 将数据传递给全局变量，进而传递给属性对话框。
	CMySheet propSheet(_T("属性对话框"));
	if(myCell->isOccupied()){ // 从数据池中获取数据到属性页上。
		int handle = myCell->getHandle();
		CLecture* pLec = &Lecture[handle];
		propSheet.page1.startMin = pLec->getStartTime();
		propSheet.page1.endMin = pLec->getEndTime();
		propSheet.page1.name = pLec->getName();
		propSheet.page1.handle = pLec->getHandle();
		propSheet.page1.week = pLec->getDate().GetDayOfWeek();
		propSheet.page1.date = pLec->getDate();
	}
	else {
		propSheet.page1.startMin = 0;
		propSheet.page1.endMin = 0;	
		propSheet.page1.name = "";
		propSheet.page1.handle = -1;
		propSheet.page1.week = col-1;
		propSheet.page1.date = dispTime + CTimeSpan(col - 1);
	}
	if (propSheet.DoModal() == IDOK) {
		for (auto i = repeatLec[col-1].begin(); i != repeatLec[col-1].end(); i++) {
			CLecture* pLec = &Lecture[*i];
			int beginRow = pLec->getStartTime() / 30;
			int endRow = (pLec->getEndTime() - 1) / 30 + 1; // 向上取整。
			CString name = pLec->getName();
			m_grid1.SetItemText(beginRow, col, name);
			for (int j = beginRow; j <= endRow; j++) {
				m_grid1.SetItemBkColour(j, col, RGB(200, 0, 0));
				CMyGridCell* myCell = (CMyGridCell*)m_grid1.GetCell(j, col);
				myCell->setHandle(*i);
				myCell->setOccupied();
			}
		}
	}
}



void CMyDialog1::OnCellNormal()
{
	m_grid1.SetCellType(1, 1, RUNTIME_CLASS(CGridCell));
	m_grid1.SetItemState(1, 1, m_grid1.GetItemState(1, 1) & ~GVIS_READONLY);
	m_grid1.Invalidate();
}

void CMyDialog1::updateDate() {
	OnCellNormal();
	int diff = dispTime.GetDayOfWeek();
	CTimeSpan span(diff - 1, 0, 0, 0);
	CTime tobeDisplay = dispTime - span;
	for (int i = 1; i <= 7; i++) {
		CString str;
		int day = tobeDisplay.GetDay();
		if (day != 1) {
			str.Format("%d  \n%s", day, week[i]);
		}
		else { str.Format("%d月  %s", tobeDisplay.GetMonth(), week[i]); }
		tobeDisplay += CTimeSpan(1, 0, 0, 0);
		m_grid1.SetItemText(0, i, str);
	}
	return;
}


void CMyDialog1::initCell() {
	OnCellNormal();
	for (int i = 0; i < m_grid1.GetRowCount(); i++) {
		for (int j = 0; j < m_grid1.GetColumnCount(); j++) {
			m_grid1.SetCellType(i, j, RUNTIME_CLASS(CMyGridCell));
		}
	}
	
}